package com.zjfgz.lib_kt_com.`fun`

import android.content.Context
import android.graphics.Color
import android.text.Spannable
import android.text.SpannableString
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.AbsoluteSizeSpan
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import android.view.WindowManager
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.zjfgz.lib_kt_com.R
import com.zjfgz.lib_kt_com.base.BaseActivity
import com.zjfgz.lib_kt_com.base.BaseFragment
import com.zjfgz.lib_kt_com.dialog.PrivacyDialog
import com.zjfgz.lib_kt_com.user.XieyiActivity


fun BaseFragment.toast(msg:String){
    Toast.makeText(activity,msg,Toast.LENGTH_SHORT).show()
}

/**
 * 显示用户协议和隐私政策
 */
fun showPrivacy(context: AppCompatActivity,clazz: Class<out XieyiActivity>, listener: () -> Unit) {
    val dialog = PrivacyDialog(context)
    val tv_privacy_tips: TextView = dialog.findViewById(R.id.tv_privacy_tips)
    val btn_exit: TextView = dialog.findViewById(R.id.btn_exit)
    val btn_enter: TextView = dialog.findViewById(R.id.btn_enter)
    dialog.show()
    val string = context.resources.getString(R.string.privacy_tips)
    val key1 = context.resources.getString(R.string.privacy_tips_key1)
    val key2 = context.resources.getString(R.string.privacy_tips_key2)
    val index1 = string.indexOf(key1)
    val index2 = string.indexOf(key2)

    //需要显示的字串
    val spannedString = SpannableString(string)
    //设置点击字体颜色
    val colorSpan1 =
        ForegroundColorSpan(context.resources.getColor(R.color.colorBlue))
    spannedString.setSpan(
        colorSpan1,
        index1,
        index1 + key1.length,
        Spannable.SPAN_EXCLUSIVE_INCLUSIVE
    )
    val colorSpan2 =
        ForegroundColorSpan(context.resources.getColor(R.color.colorBlue))
    spannedString.setSpan(
        colorSpan2,
        index2,
        index2 + key2.length,
        Spannable.SPAN_EXCLUSIVE_INCLUSIVE
    )
    //设置点击字体大小
    val sizeSpan1 =
        AbsoluteSizeSpan(16, true)
    spannedString.setSpan(
        sizeSpan1,
        index1,
        index1 + key1.length,
        Spannable.SPAN_EXCLUSIVE_INCLUSIVE
    )
    val sizeSpan2 =
        AbsoluteSizeSpan(16, true)
    spannedString.setSpan(
        sizeSpan2,
        index2,
        index2 + key2.length,
        Spannable.SPAN_EXCLUSIVE_INCLUSIVE
    )
    //设置点击事件
    val clickableSpan1: ClickableSpan =
        object : ClickableSpan() {
            override fun onClick(view: View) {
                XieyiActivity.showXieYi(context,clazz)
            }

            override fun updateDrawState(ds: TextPaint) {
                //点击事件去掉下划线
                ds.isUnderlineText = false
            }
        }
    spannedString.setSpan(
        clickableSpan1,
        index1,
        index1 + key1.length,
        Spannable.SPAN_EXCLUSIVE_INCLUSIVE
    )
    val clickableSpan2: ClickableSpan =
        object : ClickableSpan() {
            override fun onClick(view: View) {
                XieyiActivity.showYinSi(context,clazz)
            }

            override fun updateDrawState(ds: TextPaint) {
                //点击事件去掉下划线
                ds.isUnderlineText = false
            }
        }
    spannedString.setSpan(
        clickableSpan2,
        index2,
        index2 + key2.length,
        Spannable.SPAN_EXCLUSIVE_INCLUSIVE
    )

    //设置点击后的颜色为透明，否则会一直出现高亮
    tv_privacy_tips.highlightColor = Color.TRANSPARENT
    //开始响应点击事件
    tv_privacy_tips.movementMethod = LinkMovementMethod.getInstance()
    tv_privacy_tips.text = spannedString
    dialog.setOnDismissListener {
        listener.invoke()
    }
    //设置弹框宽度占屏幕的80%
    val m = context.windowManager
    val defaultDisplay = m.defaultDisplay
    val params: WindowManager.LayoutParams = dialog.getWindow()!!.getAttributes()
    params.width = (defaultDisplay.width * 0.80).toInt()
    dialog.getWindow()?.setAttributes(params)
    btn_exit.setOnClickListener { dialog.dismiss() }
    btn_enter.setOnClickListener { dialog.dismiss() }
}